<template>
  <div class="app-container">
    <!-- 顶部控制台 -->
    <div class="filter-container">
      <el-input class="filter-item" style="width: 200px; margin-left: 0" size="mini" placeholder="请输入要搜索的专题标题" v-model="form.searchTitle"/>
      <el-select class="filter-item" style="width: 160px" v-model="form.searchStatus" size="mini" @change="handlePageInit">
        <el-option v-for="item in pageData.searchStatusList" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-button type="primary" icon="el-icon-search" size="mini" @click="handlePageInit">搜索</el-button>
      <el-button type="primary" icon="el-icon-share" size="mini" @click="handleOpenSavePage">新增</el-button>
      <el-button type="success" icon="el-icon-share" size="mini" @click="handleBatchReleaseData" :disabled="form.selection.length < 1">批量发布</el-button>
    </div>
    <!--Table表格-->
    <div class="data-table">
      <el-table :data="responseData.tableList"
                border style="width: 100%;"
                v-loading="pageData.loading"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                :header-cell-style="{ background: '#545C64', color: '#ffffff' }"
                @selection-change="handleTableSelection">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column fixed="left" prop="subjectId" label="数据ID" width="260" align="center"/>
        <el-table-column prop="subjectName" label="专题名称" width="240" align="center">
          <template slot-scope="scope">
            <span class="link-type" @click="handleOpenEditPage(scope)">{{ scope.row.subjectName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="subjectImage" label="预览图" width="80" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.subjectImage != null && scope.row.subjectImage !== ''">
              <el-button size="small" width="120" type="success" @click="handleCheckInfo(scope)">预览</el-button>
            </span>
            <span v-else >无</span>
          </template>
        </el-table-column>
        <el-table-column prop="subjectStatus" label="审核状态" width="100" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.subjectStatus === 0"><el-tag type="warning">下架</el-tag></span>
            <span v-if="scope.row.subjectStatus === 1"><el-tag type="success">发布</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column prop="subjectInfo" label="专题简介" width="420" align="center"/>
        <el-table-column prop="subjectCreateTime" label="创建时间" width="120" align="center"/>
        <el-table-column fixed="right" label="操作" width="320" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleOpenEditPage(scope)">编辑</el-button>
            <el-button type="primary" size="mini" @click="handleCheckColumnInfo(scope)">查看栏目</el-button>
            <el-button v-if="scope.row.subjectStatus === 0" type="success" size="mini" @click="handleUpdateDataStatus(scope, 1)">发布</el-button>
            <el-button v-if="scope.row.subjectStatus === 1" type="warning" size="mini" @click="handleUpdateDataStatus(scope, 0)">下架</el-button>
            <el-button type="danger" size="mini" @click="handleDeleteData(scope)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页器-->
    <div style="margin-top: 20px">
      <div class="block" style="width: 100%; text-align: center">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :current-page="form.pageNum"
          :page-size="form.pageSize"
          :total="form.total"
          :page-sizes="[12, 50, 100, 200]"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange">
        </el-pagination>
      </div>
    </div>
    <!--图片预览隐藏域-->
    <div style="margin-top: 20px">
      <el-dialog title="图片预览" :visible.sync="pageData.checkPanelStatus">
        <el-card :body-style="{ padding: '0px' }">
          <div>
            <div>
              <img :src="pageData.checkPanelInfo">
            </div>
          </div>
        </el-card>
      </el-dialog>
    </div>
    <!--栏目列表隐藏域-->
    <div style="margin-top: 20px">
      <el-dialog title="专题栏目" :visible.sync="pageData.checkColumnPanelStatus" size="tiny" width="640">
        <el-card :body-style="{ padding: '0px' }">
          <div>
            <div class="filter-container">
              <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleOpenSaveColumnPage">新增栏目</el-button>
            </div>
            <div class="data-table">
              <el-table :data="responseData.columnList"
                        border style="width: 100%;"
                        v-loading="pageData.checkColumnPanelLoading"
                        element-loading-text="拼命加载中"
                        element-loading-spinner="el-icon-loading"
                        element-loading-background="rgba(0, 0, 0, 0.8)"
                        ref="multipleTable" tooltip-effect="dark" highlight-current-row>
                <el-table-column fixed="left" prop="columnId" label="栏目ID" width="260" align="center"/>
                <el-table-column prop="columnName" label="栏目名称" width="240" align="center"></el-table-column>
                <el-table-column label="可用状态" width="100" align="center">
                  <template slot-scope="scope">
                    <span v-if="scope.row.columnStatus === 0"><el-tag type="warning">未上架</el-tag></span>
                    <span v-if="scope.row.columnStatus === 1"><el-tag type="success">已上架</el-tag></span>
                  </template>
                </el-table-column>
                <el-table-column prop="columnShowIndex" label="展示顺序" width="240" align="center"></el-table-column>
              </el-table>
            </div>
          </div>
        </el-card>
      </el-dialog>
    </div>
    <!--栏目新增隐藏域-->
    <div style="margin-top: 20px">
      <el-dialog title="新增栏目" :visible.sync="pageData.saveColumnPanelStatus" size="medium"
                 :before-close="handleCloseSavePage">
        <el-card class="box-card">
          <el-form :model="form" label-width="100px" ref="form">
            <div class="text item">
              <el-form-item prop="columnName" label="栏目名称">
                <el-input placeholder="请输入栏目名称" v-model="form.columnName" class="filter-item" auto-complete="off"/>
              </el-form-item>
              <el-form-item prop="columnStatus" label="可用状态">
                <el-radio v-model="form.columnStatus" :label="0">不可用</el-radio>
                <el-radio v-model="form.columnStatus" :label="1">可用</el-radio>
              </el-form-item>
              <el-form-item prop="columnShowIndex" label="展示顺序">
                <el-input placeholder="请输入展示顺序" v-model="form.columnShowIndex" class="filter-item" auto-complete="off"/>
              </el-form-item>
              <el-form-item prop="columnData" label="内容列表">
                <el-input placeholder="请输入专题内容ID值,多个内容请使用英文逗号分隔" v-model="form.columnData" class="filter-item" auto-complete="off" type="textarea" :rows="4"/>
              </el-form-item>
              <div style="margin-top: 20px">
                <el-button type="primary" @click.native.prevent="handleSaveButtonClick('form')">增加栏目</el-button>
              </div>
            </div>
          </el-form>
        </el-card>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  // 组件引入
  // 方法工具引入
  import { PermissionsToDetectFormUser } from '@/utils/PermissionsDetect';
  import { getPermission } from '@/utils/UserCookies';
  // 接口引入
  import { loadCmsSubjectDataList,batchReleaseCmsSubjectData,loadCmsSubjectColumnDataList,saveCmsSubjectColumnData } from "@/api/content";
  export default {
    name: "subject",
    //import引入的组件需要注入到对象中才能使用
    components: {},
    data() {
      return {
        pageData:{
          searchStatusList: [
            {label: "发布状态", value: -1},
            {label: "下架", value: 0},
            {label: "发布", value: 1}
          ],// 发布状态预设选项
          loading: false,
          checkPanelStatus: false,// 图片预览隐藏域
          checkPanelInfo: "",
          checkColumnPanelStatus: false,// 栏目预览隐藏域
          checkColumnPanelLoading: false,
          saveColumnPanelStatus: false,// 栏目新增隐藏域
        },// 页面配置数据
        form:{
          searchTitle: "",
          searchStatus: -1,
          pageNum: 1,// 表单--页码
          pageSize: 12,// 表单--页面
          total: 0,// 表单--总量
          selection: [],// 多选的内容
          subjectId: "",// 请求栏目新增的时候传递的专题ID
          columnName: "",
          columnStatus: 0,
          columnShowIndex: 0,
          columnData: "",
        },// 页面表单
        formRules: {},// 表单验证
        responseData: {
          tableList: [],
          columnList: [],
        },// 接口响应数据
      }
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //方法集合
    methods:{
      // 事件函数
      handlePageInit(){
        this.form.pageNum = 1;
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsSubjectDataList();// 读取路由角色列表
      },// 页面初始化事件
      handleCurrentChange(val){
        this.form.pageNum = val;// 更换页码
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsSubjectDataList();// 读取路由角色列表
      },// 页码切换事件
      handleSizeChange(val){
        this.form.pageSize = val;// 更换页面大小
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsSubjectDataList();// 读取路由角色列表
      },// 页面size切换事件
      handleTableSelection(val){
        let list = [];// 初始化多选数组
        for (let i=0; i<val.length; i++){
          list.push(val[i].subjectId);
        }// 遍历多选的列表，将ID拼入数组中
        this.form.selection = list;// 进行多选值的切换
      },// 表格多选事件
      handleOpenSavePage(){
        // 判断该用户是否有更改发布状态的权限
        if (PermissionsToDetectFormUser(getPermission(), '450001')){
          this.$jump.push({path: "/content/SubjectSave"});
        } else {
          this.$message.warning("您的权限不足，无法使用此功能，请联系管理员");
        }
      },// 打开新增页面
      handleBatchReleaseData(){
        // 判断该用户是否有更改发布状态的权限
        if (PermissionsToDetectFormUser(getPermission(), '450005')){
          this.$confirm("是否更改当前所选数据的发布状态?", "提示", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let param = {
              subjectId: this.form.selection.toString(),
              subjectStatus: 1
            };
            this.batchReleaseCmsSubjectData(param);
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 批量发布事件
      handleOpenEditPage(scope){
        if (PermissionsToDetectFormUser(getPermission(), "450003")){
          this.$jump.push({path: "/content/SubjectEdit",query: {subjectId: scope.row.subjectId}});
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开修改页面
      handleCheckInfo(scope){
        this.pageData.checkPanelStatus = true;
        this.pageData.checkPanelInfo = scope.row.subjectImage;
      },// 预览图像(查看表格详情)
      handleCheckColumnInfo(scope){
        this.pageData.checkColumnPanelStatus = true;
        this.pageData.checkColumnPanelLoading = true;
        this.form.subjectId = scope.row.subjectId;
        let prams = {subjectId: scope.row.subjectId};
        this.loadCmsSubjectColumnDataList(prams);
      },// 栏目查看隐藏域
      handleUpdateDataStatus(scope, status){
        if (PermissionsToDetectFormUser(getPermission(), "450005")){
          this.$confirm("是否更改当前所选数据的发布状态?", "提示", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let param = {
              subjectId: scope.row.subjectId,
              subjectStatus: status
            };
            this.batchReleaseCmsSubjectData(param);
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 更改内容发布状态
      handleDeleteData(scope){
        this.$message.info("该功能暂未开放，如有需要请联系管理员");
      },// 删除内容
      handleOpenSaveColumnPage(){
        if (PermissionsToDetectFormUser(getPermission(), "450003")){
          this.pageData.checkColumnPanelStatus = false;// 关闭栏目查看面板
          this.pageData.saveColumnPanelStatus = true;// 打开栏目新增面板
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开栏目新增面板
      handleCloseSavePage(){
        this.$confirm("确认关闭？").then(response => {
          // 清空表单
          this.form.subjectId = "";
          this.form.columnName = "";
          this.form.columnStatus = 0;
          this.form.columnShowIndex = 0;
          this.form.columnData = "";
          // 关闭面板
          this.pageData.saveColumnPanelStatus = false;
        }).catch(err => {
          console.log(err);
        });
      },// 关闭栏目新增面板监听
      handleSaveButtonClick(formName){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (PermissionsToDetectFormUser(getPermission(), "450001")){
              this.$confirm('确认保存当前数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                this.saveCmsSubjectColumnData();
              }).catch(close => {
                console.log(close);
                this.$message.info("已取消操作");
              });
            } else {
              this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
            }
          }
        });
      },// 保存栏目数据
      // 接口
      loadCmsSubjectDataList(){
        loadCmsSubjectDataList(this.form).then(response => {
          if (response.code === "200"){
            this.pageData.loading = false;
            this.responseData.tableList = response.data.list;
            this.form.total = response.data.total;
          } else {
            this.$message.warning(response.message);
          }
        }).catch( error =>{
          console.log(error);
          this.$message.error(error);
        })
      },// 获取数据列表
      batchReleaseCmsSubjectData(param){
        batchReleaseCmsSubjectData(param).then(response => {
          if (response.code === "200"){
            this.$message.success(param.subjectStatus === 1 ? "发布成功" : "下架成功");
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(close => {
          console.log(close);
        });
      },// 批量发布数据
      loadCmsSubjectColumnDataList(prams){
        loadCmsSubjectColumnDataList(prams).then(response => {
          if (response.code === "200"){
            this.responseData.columnList = response.data;
            this.pageData.checkColumnPanelLoading = false;
          } else {
            this.$message.warning(response.message);
          }
        }).catch(close => {
          console.log(close);
        });
      },// 获取专题栏目列表
      saveCmsSubjectColumnData(){
        saveCmsSubjectColumnData(this.form).then(response => {
          if (response.code === "200"){
            this.$message.success("栏目保存成功");
            // 清空表单
            this.form.subjectId = "";
            this.form.columnName = "";
            this.form.columnStatus = 0;
            this.form.columnShowIndex = 0;
            this.form.columnData = "";
            // 关闭面板
            this.pageData.saveColumnPanelStatus = false;
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(close => {
          console.log(close);
        });
      }// 保存专题栏目数据
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created: function () {
      let _this = this;
      document.onkeydown = function(e) {
        if (!_this.pageData.loading) {
          let key = window.event.keyCode;
          if (key === 13) {
            _this.handlePageInit(); //此方法是当按下enter键后要做的动作。
          }
        }
      };
      setTimeout(function () {
        _this.handlePageInit();
      }, 800);
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {},
    beforeCreate() {}, //生命周期 - 创建之前
    beforeMount() {}, //生命周期 - 挂载之前
    beforeUpdate() {}, //生命周期 - 更新之前
    updated() {}, //生命周期 - 更新之后
    beforeDestroy() {}, //生命周期 - 销毁之前
    destroyed() {}, //生命周期 - 销毁完成
    activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  };
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
